#include "engine.h"
#include <vector>
#include <QtDebug>
#include <memory>
using namespace std;
void Engine::find_next_pos(const Chess& c,int player,vector <Step>& futurechess,int * chessborad) {
    for (int i = 0;i < 8;i++) {
        int chesspos = c.i + dection[i];
        while(chesspos < 144 && chesspos >= 0 && chessborad[chesspos] == 2){
            chessborad[c.i] = 2;
            chessborad[chesspos] = player;
            for (int j = 0;j < 8;j++){
                int hinderpos = chesspos + dection[j];
                while(hinderpos < 144 && hinderpos >= 0 && chessborad[hinderpos] == 2){
                    futurechess.push_back(Step(c.i,chesspos,hinderpos));
                    hinderpos += dection[j];
                }
            }
            chessborad[c.i] = player;
            chessborad[chesspos] = 2;
            chesspos += dection[i];
        }
    }
}



void Engine::find_sum_step(int player, vector<Step> &nextstep,Cb_with_num &cb) {
    if (player == blackc){
        for (int i = 0;i < 4 ;i++) {
            find_next_pos(cb.movechess.blackchess[i],player,nextstep,cb.chessborad);
        }
    }
    else{
        for (int i = 0;i < 4 ;i++) {
            find_next_pos(cb.movechess.whitechess[i],player,nextstep,cb.chessborad);
        }
    }
}

int Engine::dim(Cb_with_num &cb)
{
    int bd = 0,wd = 0;
    for(int i = 0;i < 4;i++){
        for(int j = 0;j < 8;j++){
            if(cb.chessborad[cb.movechess.blackchess[i].i + dection[j]] == nochess){
                bd++;
                if(cb.chessborad[cb.movechess.blackchess[i].i + 2*dection[j]] == nochess)
                    bd++;
            }
            if(cb.chessborad[cb.movechess.whitechess[i].i + dection[j]] == nochess){
                wd++;
                if(cb.chessborad[cb.movechess.whitechess[i].i + 2*dection[j]] == nochess)
                    wd++;
            }
        }
    }
    if(*player == blackc) return bd-wd;
    else return wd-bd;
}
int Engine::ef(const Step &s,int player,Cb_with_num &cb){
    if(has_return) return 0;
    temp_set(s,player,cb);
    char king = 0,queen = 0,dim = 0;
    if(if_use_hash) {
        Score_struct score = find_score(cb);
        king = score.king,queen = score.queen;
    }
    else{
        if(krt) king = king_move(cb);
        if(qrt) queen = queen_move(cb);
        if(drt) dim = (char)Engine::dim(cb);
    }
    temp_back(s,player,cb);
    return king * krt + queen *qrt + drt * dim;
}

char Engine::get_krt(int chess_num){
    if(!computer_remote){
        if(chess_num < stage1) return king_ratio1;
        else if(chess_num <= stage2) return king_ratio2;
        else if(chess_num < stage3) return king_ratio3;
        else if(chess_num < stage4) return king_ratio4;
        else if(chess_num < stage5) return king_ratio5;
        else if(chess_num < stage6) return king_ratio6;
        else if(chess_num < stage7) return king_ratio7;
        else return king_ratio8;
    }
    else {
        if(chess_num < stage1_2) return king_ratio1_2;
        else if(chess_num <= stage2_2) return king_ratio2_2;
        else if(chess_num < stage3_2) return king_ratio3_2;
        else if(chess_num < stage4_2) return king_ratio4_2;
        else if(chess_num < stage5_2) return king_ratio5_2;
        else if(chess_num < stage6_2) return king_ratio6_2;
        else if(chess_num < stage7_2) return king_ratio7_2;
        else return king_ratio8_2;
    }
}

char Engine::get_qrt(int chess_num){
    if(!computer_remote){
        if(chess_num < stage1) return queen_ratio1;
        else if(chess_num <= stage2) return queen_ratio2;
        else if(chess_num < stage3) return queen_ratio3;
        else if(chess_num < stage4) return queen_ratio4;
        else if(chess_num < stage5) return queen_ratio5;
        else if(chess_num < stage6) return queen_ratio6;
        else if(chess_num < stage7) return queen_ratio7;
        else return queen_ratio8;
    }
    else {
        if(chess_num < stage1_2) return queen_ratio1_2;
        else if(chess_num <= stage2_2) return queen_ratio2_2;
        else if(chess_num < stage3_2) return queen_ratio3_2;
        else if(chess_num < stage4_2) return queen_ratio4_2;
        else if(chess_num < stage5_2) return queen_ratio5_2;
        else if(chess_num < stage6_2) return queen_ratio6_2;
        else if(chess_num < stage7_2) return queen_ratio7_2;
        else return queen_ratio8_2;
    }
}

char Engine::get_drt(int chess_num){
    if(!computer_remote){
        if(chess_num < stage1) return dim_ratio1;
        else if(chess_num <= stage2) return dim_ratio2;
        else if(chess_num < stage3) return dim_ratio3;
        else if(chess_num < stage4) return dim_ratio4;
        else if(chess_num < stage5) return dim_ratio5;
        else if(chess_num < stage6) return dim_ratio6;
        else if(chess_num < stage7) return dim_ratio7;
        else return dim_ratio8;
    }
    else {
        if(chess_num < stage1_2) return dim_ratio1_2;
        else if(chess_num <= stage2_2) return dim_ratio2_2;
        else if(chess_num < stage3_2) return dim_ratio3_2;
        else if(chess_num < stage4_2) return dim_ratio4_2;
        else if(chess_num < stage5_2) return dim_ratio5_2;
        else if(chess_num < stage6_2) return dim_ratio6_2;
        else if(chess_num < stage7_2) return dim_ratio7_2;
        else return dim_ratio8_2;
    }
}
